<?php

class Chuyen_muc_bai_vietsController extends BaseController {

    /**
     * Chuyen_muc_bai_viet Repository
     *
     * @var Chuyen_muc_bai_viet
     */
    protected $chuyen_muc_bai_viet;

    public function __construct(Chuyen_muc_bai_viet $chuyen_muc_bai_viet) {
        $this->chuyen_muc_bai_viet = $chuyen_muc_bai_viet;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index() {
        $chuyen_muc_bai_viets = $this->chuyen_muc_bai_viet->all();

        return View::make('chuyen_muc_bai_viets.index', compact('chuyen_muc_bai_viets'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create() {
        return View::make('chuyen_muc_bai_viets.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store() {
        $input = Input::all();
        $validation = Validator::make($input, Chuyen_muc_bai_viet::$rules);

        if ($validation->passes()) {
            $this->chuyen_muc_bai_viet->create($input);

            return Redirect::route('categories.index');
        }

        return Redirect::route('categories.create')
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id) {
        $chuyen_muc_bai_viet = $this->chuyen_muc_bai_viet->findOrFail($id);

        return View::make('chuyen_muc_bai_viets.show', compact('chuyen_muc_bai_viet'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id) {
        $chuyen_muc_bai_viet = $this->chuyen_muc_bai_viet->find($id);

        if (is_null($chuyen_muc_bai_viet)) {
            return Redirect::route('categories.index');
        }

        return View::make('chuyen_muc_bai_viets.edit', compact('chuyen_muc_bai_viet'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id) {
        $chuyen_muc = $this->chuyen_muc_bai_viet->findOrFail($id);
        $current_chuyen_muc = $chuyen_muc['ten_chuyen_muc'];
        
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Chuyen_muc_bai_viet::$rules);

        if($input['ten_chuyen_muc'] == $current_chuyen_muc){
            return Redirect::route('categories.show', $id);

        }else{
            if ($validation->passes()) {
                $chuyen_muc_bai_viet = $this->chuyen_muc_bai_viet->find($id);
                $chuyen_muc_bai_viet->update($input);
                return Redirect::route('categories.show', $id);
            }     
        }

        return Redirect::route('categories.edit', $id)
                        ->withInput()
                        ->withErrors($validation)
                        ->with('message', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id) {
        $this->chuyen_muc_bai_viet->find($id)->delete();
return 1;
        //return Redirect::route('categories.index');
    }

}
